package show.me.the.money.config.reportSchema;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Element;

import show.me.the.money.config.reportSchema.define.ReportSchema;
import show.me.the.money.config.schema.SchemaItem;
import show.me.the.money.util.context.Context;
import show.me.the.money.util.context.HashMapContext;
import show.me.the.money.util.exp.ExpRunner;


public class EvalReportDefine implements ReportDefine {
	private List<SchemaItem> headers = new ArrayList<SchemaItem>();
	private int startRenderIndex;
	@SuppressWarnings("unchecked")
	public void setDefineXML(Element define) {
		List<Element> els = define.selectNodes("headers/item");
		int  i = startRenderIndex;
		for(Element el : els){
			int index = Integer.parseInt(el.attributeValue("renderIndex",String.valueOf(i)));
			SchemaItem si = new SchemaItem(el,index);
			if(si.isEvalValue()){
				headers.add(si);
			}
			i ++;
		}
	}
	
	@SuppressWarnings("unchecked")
	public void addCondition(List topCnd) {
		
	}

	public List<SchemaItem> getHeaders(boolean group) {
		List<SchemaItem> ls = new ArrayList<SchemaItem>();
		for(SchemaItem i : headers){
			ls.add(i);
		}
		return ls;
	}
	
	public int getHeaderCount() {
		return headers.size();
	}
	
	public void setStartRenderIndex(int start) {
		startRenderIndex = start;
	}

	@SuppressWarnings("unchecked")
	public void runMutiMode(HashMap<String, HashMap> data, Context ctx) {
		for(HashMap r : data.values()){
			Context rCtx = new HashMapContext(r);
			ctx.putCtx("r", rCtx);
			for(SchemaItem h : headers){
				String id = h.getId();
				Object v = ExpRunner.run(h.exp(), ctx);
				r.put(id, v);
			}

		}
	}

	@SuppressWarnings("unchecked")
	public List<HashMap> runSingleMode(Context ctx) {
		List<HashMap> data = new ArrayList<HashMap>();
		return data;
	}

	public void setQueryArgs(List<SchemaItem> queryArgs) {
		// TODO Auto-generated method stub

	}

	public void setReportSchema(ReportSchema rs) {
	}

}
